package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.*;
import com.cskaoyan.catmall.model.bo.BaseParamBo;
import com.cskaoyan.catmall.model.bo.UserListBO;
import com.cskaoyan.catmall.model.bo.UserUpdateBO;
import com.cskaoyan.catmall.model.vo.*;
import com.cskaoyan.catmall.service.UserServiceZyy;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @date: 2022-01-07-15-45
 * @author: ZYY
 * @description: 用户的Service层的实现类
 */
@Service
public class UserServiceImpZyy implements UserServiceZyy {
    @Autowired
    MarketUserMapper userDao;

    @Autowired
    MarketFootprintMapper footprintMapper;

    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;

    @Autowired
    MarketFeedbackMapper feedbackMapper;

    /**
     * @author:zyy
     * @description: 用户界面list功能的查询语句
     * @date:2022/01/07 16:26
     * @param param 常见的页面配置信息，
     * @param userListBO 输入的数据（用户名，id，手机号）
     * @return java.util.List<com.cskaoyan.catmall.model.vo.UserListVO>
     */
    @Override
    public UserDataVO list(BaseParamBo param, UserListBO userListBO) {
        Integer limit = param.getLimit(); //数据量

        Integer page = param.getPage(); // 页码
        String order = param.getOrder(); //排序列
        String sort = param.getSort(); // 排序规则
        // 使用PageHelper
        // 开启分页功能，传入页码和数据量
        PageHelper.startPage(page, limit);

        // 查询数据库
        Integer id = userListBO.getId();
        String username = userListBO.getUsername();
        String mobile = userListBO.getMobile();
        List<UserListVO> list = userDao.select(username, mobile, sort, order);

        // 获得PageHelper封装到PageInfo中得到的数据
        PageInfo<UserListVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        UserDataVO userDataVO = UserDataVO.data(total, pages, limit, page, list);
        return userDataVO;
    }

    /**
     * @author:zyy
     * @description: 根据id查询到对应的数据
     * @date:2022/01/07 19:23
     * @param id
     * @return com.cskaoyan.catmall.model.vo.UserListVO
     */
    @Override
    public UserListVO detail(Integer id) {
        UserListVO userListVO = userDao.selectById(id);
        return userListVO;
    }

    /**
     * @author:zyy
     * @description: 修改会员的信息
     * @date:2022/01/07 19:59
     * @param userUpdateBO
     * @return int
     */
    @Override
    public int update(UserUpdateBO userUpdateBO) {
        int i = userDao.update(userUpdateBO);
        return i;
    }

    @Override
    public FootPrintVO footPrintList(BaseParamBo param, Integer userId, Integer goodsId) {

        Integer limit = param.getLimit(); //数据量
        Integer page = param.getPage(); // 页码
        String order = param.getOrder(); //排序列
        String sort = param.getSort(); // 排序规则
        // 使用PageHelper
        // 开启分页功能，传入页码和数据量
        PageHelper.startPage(page, limit);

        List<FootPrintListVO> list = footprintMapper.selectFootPrintSelective(userId, goodsId, sort, order);

        // 获得PageHelper封装到PageInfo中得到的数据
        PageInfo<FootPrintListVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        FootPrintVO data = FootPrintVO.data(total, pages, limit, page, list);
        return data;
    }

    @Override
    public HistoryVO historyList(BaseParamBo param, Integer userId, String keyWord) {
        Integer limit = param.getLimit(); //数据量
        Integer page = param.getPage(); // 页码
        String order = param.getOrder(); //排序列
        String sort = param.getSort(); // 排序规则
        // 使用PageHelper
        // 开启分页功能，传入页码和数据量
        PageHelper.startPage(page, limit);

        List<HistoryListVO> list = searchHistoryMapper.selectSearchHistorySelective(userId, keyWord, sort, order);

        // 获得PageHelper封装到PageInfo中得到的数据
        PageInfo<HistoryListVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        HistoryVO data = HistoryVO.data(total, pages, limit, page, list);
        return data;
    }

    @Override
    public FeedBackVO feedbackList(BaseParamBo param, String username, Integer id) {
        Integer limit = param.getLimit(); //数据量
        Integer page = param.getPage(); // 页码
        String order = param.getOrder(); //排序列
        String sort = param.getSort(); // 排序规则
        // 使用PageHelper
        // 开启分页功能，传入页码和数据量
        PageHelper.startPage(page, limit);

        List<FeedBackListVO> list = feedbackMapper.selectSearchFeedbackSelective(username, id, sort, order);

        // 获得PageHelper封装到PageInfo中得到的数据
        PageInfo<FeedBackListVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        FeedBackVO data = FeedBackVO.data(total, pages, limit, page, list);
        return data;
    }
}